﻿
/*============== simpleIRC - http://matthias.van-eeghem.com/ ==============*
 *                                                                         *
 *  File: QueryList.cs                                                        *
 *  Project: simpleIRC                                                     *
 *  Author(s): Matthias Van Eeghem <matthias@van-eeghem.com>               *
 *                                                                         *
 * ========================================================================*
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
===========================================================================*/

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace simpleIRC.UI
{
    /// <summary>
    /// The QueryList class
    /// </summary>
    public class QueryList
    {
        /// <summary>
        /// This variable holds a reference to the MainWindow class
        /// </summary>
        MainWindow mainWindow = null;

        /// <summary>
        /// The ServerIndex the QueryList is on
        /// </summary>
        private int ServerIndex;

        /// <summary>
        /// This TreeViewItem holds the TreeViewItem for this QueryListWindow object
        /// </summary>
        public TreeViewItem QueryListTreeViewItem { get; private set; }

        /// <summary>
        /// This static variable holds a list of all the QueryLists created
        /// </summary>
        public static List<QueryList> ListOfQueries = new List<QueryList>();

        /// <summary>
        /// The constructor of the QueryList class
        /// </summary>
        /// <param name="serverIndex">The serverindex this QueryList is linked to</param>
        /// <param name="parentWindow">A reference to the MainWindow object</param>
        
        public QueryList(int serverIndex, MainWindow parentWindow)
        {
            mainWindow = parentWindow;
            ServerIndex = serverIndex;

            ListOfQueries.Add(this);
        }

        /// <summary>
        /// This function adds a new query list to a server.
        /// </summary>

        public void CreateQueryList()
        {
            QueryListTreeViewItem = new TreeViewItem();
            QueryListTreeViewItem.Header = "Query: 0";
            QueryListTreeViewItem.Name = "Server" + ServerIndex.ToString();
            QueryListTreeViewItem.Visibility = Visibility.Collapsed;
            QueryListTreeViewItem.ExpandSubtree();

            TreeViewItem QueryListTreeViewItemCopy = QueryListTreeViewItem;
            mainWindow.UserInterface.AddRightClickEvent(ref QueryListTreeViewItemCopy, "Close All", mainWindow.UserInterface.ContextMenu_CloseAllQueries);

            // Add the treeview item to the treeviewlist.
            mainWindow.ChannelsTreeView.Items.Add(QueryListTreeViewItem);
        }

        /// <summary>
        /// This function removes the query list and all query objects from the ChannelsTreeView
        /// </summary>

        public void RemoveQueryList()
        {
            int TimesLooped = 0, Count = Core.QueryList.Count;

            // Loop through all the queries
            for (int i = 0; i < Count; i++)
            {
                Query Query = Core.QueryList[i];

                if (Query != null && Query.Server.Index == ServerIndex)
                {
                    QueryWindow.List[Query.Index].Remove();
                    Query.Remove();
                }

                TimesLooped++;
            }

            QueryListTreeViewItem.Visibility = Visibility.Collapsed;
        }
    }
}
